﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using CanDoo.Web;
using CanDoo.Extensions;

using System.ComponentModel.DataAnnotations;
using CanDoo.Admin.Core.Models;
using Microsoft.AspNet.Identity.Owin;
using CanDoo.Core.Data;
using Microsoft.Owin;
using Autofac.Integration.Owin;

namespace CanDoo.Admin.Core.Identity
{
    public class CandooClaimsIdentityFactory : ClaimsIdentityFactory<User,long>
    {
        internal const string IdentityProviderClaimType = "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
        internal const string DefaultIdentityProviderClaimValue = "ASP.NET Identity";

        public CandooClaimsIdentityFactory()
        {
            UserIdClaimType = ClaimTypes.NameIdentifier;
            UserNameClaimType = ClaimsIdentity.DefaultNameClaimType;

            NickName = "NickName";
            Name = "Name";
            UserType = "UserType";
            UserRoleIds = "UserRoleIds";
            LastLoginTimeType = "LastLoginTimeType";

        }

        public async override Task<ClaimsIdentity> CreateAsync(UserManager<User, long> manager, User user, string authenticationType)
        {
            // This is a standard place where you can register your ClaimsIdentityFactory.
            // This class generates a ClaimsIdentity for the given user
            // By default it adds Roles, UserName, UserId as Claims for the User
            // You can add more standard set of claims here if you want to.
            // The Following sample shows how you can add more claims in a centralized way.
            // This sample does not add Roles as Claims

            

            var id = new ClaimsIdentity(authenticationType, UserNameClaimType, null);
            id.AddClaim(new Claim(UserIdClaimType, user.Id.ToString(), ClaimValueTypes.String));
            id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String));
            id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String));

            var name = user.Name ?? user.UserName;
            id.AddClaim(new Claim(Name, name, ClaimValueTypes.String));
            id.AddClaim(new Claim("UserType", user.UserType.ToString(), ClaimValueTypes.String));


                var UserRoleIds = "";
                if (user.Roles != null)
                {
                    foreach (var m in user.Roles)
                    {
                        UserRoleIds += m.Id + ",";
                    }
                    if (UserRoleIds != "")
                        UserRoleIds = UserRoleIds.Substring(0, UserRoleIds.Length - 1);
                }
                id.AddClaim(new Claim("UserRoleIds", UserRoleIds, ClaimValueTypes.String));


            //var CollegeId = Web.Session.Read<long>("CollegeId");
            //var AloneResearchDeptId = Web.Session.Read<long>("AloneResearchDeptId"); 
            //var DepartmentId = Web.Session.Read<long>("DepartmentId");



            //var CollegeId = Web.Session.Read<long>("CollegeId");
            //var AloneResearchDeptId = Web.Session.Read<long>("AloneResearchDeptId");
            //var DepartmentId = Web.Session.Read<long>("DepartmentId");

            //Web.Session.Write<long?>("CollegeId", null);
            //Web.Session.Write<long?>("AloneResearchDeptId", null);
            //Web.Session.Write<long?>("DepartmentId", null);

            //Web.Session.Write<long>("CollegeId", 0L);
            //Web.Session.Write<long>("AloneResearchDeptId", 0L);
            //Web.Session.Write<long>("DepartmentId", 0L);


            //测试时临时给定参数
            //id.AddClaim(new Claim("CollegeId", "30292341780647943", ClaimValueTypes.String));
            //id.AddClaim(new Claim("AloneResearchDeptId", "30292343881994249", ClaimValueTypes.String));
            //id.AddClaim(new Claim("DepartmentId", "30292341495435275", ClaimValueTypes.String));

            //id.AddClaim(new Claim(NickName, user.NickName, ClaimValueTypes.String));
            //id.AddClaim(new Claim(FullName, user.FullName, ClaimValueTypes.String));
            //id.AddClaim(new Claim(UserGroup, user.UserGroup, ClaimValueTypes.String));
            //id.AddClaim(new Claim(UserTable, user.UserTable, ClaimValueTypes.String));
            //id.AddClaim(new Claim(UserID, user.UserTable, ClaimValueTypes.String));

            //id.AddClaim(new Claim("CollegeId", "30292341780647943", ClaimValueTypes.String));
            //id.AddClaim(new Claim("AloneResearchDeptId", "30292343881994249", ClaimValueTypes.String));
            //id.AddClaim(new Claim("DepartmentId", "30292341495435275", ClaimValueTypes.String));

            id.AddClaim(new Claim(LastLoginTimeType, System.DateTime.Now.ToString(), ClaimValueTypes.DateTime));

            if (manager.SupportsUserClaim)
            {
                id.AddClaims(await manager.GetClaimsAsync(user.Id));
            }
            return id;
        }

        public string NickName { get; set; }

        public string Name { get; set; }
        public string UserType { get; set; }

        public string LastLoginTimeType { get; set; }
        public string UserRoleIds { get; set; }
    }
}
